Imports System.Web.Security
Imports MIMS.WRS.Business
Imports MIMS.WRS.Data
Imports MIMS.WRS.Entities


Public Class CreateShoppingUser
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label

    Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents lnkForgotYourPassword As System.Web.UI.WebControls.LinkButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub


    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        btnCancel.Attributes.Add("onclick", "javascript:window.close();return false;")
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim userID As Integer
        Dim user As New ShoppingUser

        Try
            user.Email = txtUsername.Text.Trim()
            User.Password = Utilities.HashPassword(txtPassword.Text)

            Dim shopingUserId As Int32 = ShoppingListService.Login(txtUsername.Text.Trim(), Utilities.HashPassword(txtPassword.Text))
            If (shopingUserId <> 0) Then
                userID = shopingUserId

                'Logged in successfully. check to confirm for save/overwrite shopping item
                Dim conPointID As String = ""
                Try
                    conPointID = Convert.ToString(Session(Constant.SS_CONPOINT_ID))
                Catch ex As Exception
                    conPointID = ""
                End Try
                If (conPointID.Length > 0 AndAlso conPointID <> "0") Then

                    Dim shoppingOrderCol As ShoppingOrderCollection = ShoppingListService.GetShoppingOrderByShoppingUserId(shopingUserId)
                    Dim shoppingOrder As New shoppingOrder
                    Dim bExisted As Boolean = False
                    If (Not shoppingOrderCol Is Nothing AndAlso shoppingOrderCol.Count > 0) Then
                        For Each item As ShoppingOrder In shoppingOrderCol
                            If (conPointID = item.ConPointID.ToString()) Then
                                shoppingOrder = item
                                ShoppingConext.ShoppingOrderId = shoppingOrder.ShoppingOrderId
                                bExisted = True
                                Exit For
                            End If
                        Next
                    End If
                    If (bExisted) Then
                        ShoppingConext.ShopingUserId = userID
                        ShoppingConext.ShopingUserEmail = user.Email
                        Response.Redirect(String.Format("ConfirmToOverwriteOrAddToExistedList.aspx?ConPointID={0}&userid={1}&useremail={2}", conPointID, userID, user.Email))
                    End If

                    'Dim shoppingOrderItem As ShoppingOrder = ShoppingListService.GetShoppingOrderByShoppingUserId(shopingUserId)
                    'If (shoppingOrderItem Is Nothing AndAlso Not shoppingOrderItem.ShoppingOrderId > 0 AndAlso shoppingOrderItem.ConPointID.ToString() <> conPointID) Then
                    '    Response.Redirect(String.Format("ConfirmToOverwriteShopplingList.aspx?CurConPointID={0}&ConPointID={1}&userid={2}&useremail={3}", conPointID, shoppingOrderItem.ConPointID.ToString(), userID, user.Email))
                    'End If
                End If
            Else
                If (ShoppingListService.IsExistedEmail(txtUsername.Text.Trim())) Then
                    lblErr.Text = "Invalid password. Please try again."
                    Return
                End If
                userID = ShoppingListService.CreateShoppingUser(user)
            End If

        Catch ex As Exception
            If (ShoppingListService.IsExistedEmail(txtUsername.Text.Trim())) Then
                lblErr.Text = "Invalid password. Please try again."
                Return
            End If
            Try
                userID = ShoppingListService.CreateShoppingUser(user)
            Catch ex1 As Exception
            End Try
        End Try
        If (userID > 0) Then
            ShoppingConext.ShopingUserId = userID
            ShoppingConext.ShopingUserEmail = user.Email
            Page.RegisterStartupScript("windowClose", "<script language='javascript'>windowCloseAndRefresh1();</script>")
        End If
    End Sub

    Private Sub lnkForgotYourPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkForgotYourPassword.Click
        Response.Redirect("ForgotYourPassword.aspx")
    End Sub
End Class
